#!/usr/bin/python
# coding: iso-8859-2
import sys
import re
import math
from collections import defaultdict



class VectorAnalysis:

	def __init__(self, utf, texts, start_text, clp, clp_type):
		#text_name, text_info, text in texts:

		"""
		clp_type:
				X - all\n
				A - rzeczownik\n
				B - czasownik\n
				C - przymiotnik\n
				D - liczebnik\n
				E - zaimek\n
				F - przyslowek\n
				G - nieodmienny\n
		"""
		
		self.utf = utf
		self.texts = texts
		self.start_text = start_text
		self.clp = clp
		if clp == True:
			from wrapper import *
			clp_path = "path_to_clp_library"
			self.clp_wrapper = Wrapper(clp_path)
			
		self.DF = defaultdict(int)
		
		print self.texts
		print self.start_text
		self.vector_l  = self.initialize_vectors(self.texts)
		
		
		
	
	def decode(self, text):
		""" return word list"""
		if self.utf == False:
			tekst = text.decode("ISO-8859-2")
			pat = re.compile(r'\W+', re.UNICODE)
		else:
			tekst = text
			pat = re.compile(r'[^a-zA-ZążśźęćńółĄŻŚŹĘĆŃÓŁ]+')

		slowa = pat.split(tekst)
		
		return slowa
		
	
	def init_vector(self, word_list):
		vector = defaultdict(int) #initialized by 0 int
		for word in word_list:
		
			#use AGH clp module
			if self.clp:
				if utf:
					clp_list =  self.clp_wrapper.rec_utf(word)
				else:
					clp_list =  self.clp_wrapper.rec(word)
				
				if len(clp_list)>0:
					for id in clp_list:
						if self.clp_type =='X':
							vector[id] += 1
						else:
							label = self.clp_wrapper.label(id)
							if label[0] in self.clp_type:
								vector[id] += 1		
				
			# DO  NOT USE CLP
			else:
				vector[word] += 1


		for key in vector.keys():
			self.DF[key] +=1
		
		return vector


	def cosinus(self, x,y):
		sum_xy = 0.0
		sum_x2 = 0.0
		sum_y2 = 0.0
		
		for w in x.keys():
			sum_xy += x[w]*y[w]
			sum_x2 += math.pow(x[w] ,2)
			
		for w in y.keys():
			sum_y2 += math.pow(y[w], 2)
		return sum_xy/( math.sqrt(sum_x2  ) * math.sqrt( sum_y2) )

	
	def initialize_vectors(self, texts):
		
		vector_list = []
		for text_name, text_info, text in texts:
			vector = self.init_vector(self.decode(text))
			print "add to vector: %s" % text_name
			vector_list.append( (text_name, text_info, vector))
			
		# Normalized vectors
		for name, info, vec in vector_list:
			for key in vec.keys():
				vec[key] *= math.log(1.0 * len(vector_list) /self.DF[key]) 
		
		#:print vector_list
		return vector_list
		
	def calculate(self):
		hash = {}
		s_vec = self.vector_l[0][2]
		for s_name, s_info, vec1 in self.vector_l:
				if s_info == self.start_text:
					s_vec = vec1
		
		for name, info, vec in self.vector_l:
		  			hash[info] = self.cosinus(s_vec, vec)*100.0
		
		sd = sorted(hash.iteritems(), key = lambda(k,v) : (v, k), reverse = True)
		return sd
